<template>
  <div
    class="context-menu"
    v-show="show"
    :style="{ top: top + 'px', left: left + 'px' }"
    v-clickoutside="onClickoutside"
  >
    <div class="x6-menu">
      <component
        :is="currentComponent"
        :data="componentData"
        @close="show = false"
        @delete="onDelete"
      >
      <slot />
      </component>
    </div>
  </div>
</template>

<script>
import clickoutside from "../../utils/clickoutside";
import nodeContextMenu from "./node-context-menu";
import edgeContextMenu from "./edge-context-menu";
import { getGraph } from "../graph-common/graph";
export default {
  directives: { clickoutside },
  components: {
    nodeContextMenu,
  },
  data() {
    return {
      currentComponent: nodeContextMenu,
      show: false,
      top: 0,
      left: 0,
      data: {},
      componentData: {},
    };
  },
  methods: {
    init(data) {
      this.data = data;
      this.top = data.e.pageY;
      this.left = data.e.pageX;
      this.componentData = data.cell.data;
      switch (data.cell.shape) {
        //节点
        case "rtNode":
          this.currentComponent = nodeContextMenu;
          break;
        //边
        case "rtEdge":
          this.currentComponent = edgeContextMenu;
          break;
      }
    },
    onClickoutside() {
      this.show = false;
    },
   
    onDelete() {
    
      const graph = getGraph();
      graph.removeCells([this.data.cell]);
      this.show = false;
    },
  },
};
</script>

<style scoped lang="less">
.context-menu {
  position: absolute;
  z-index: 1;
  // background-color: #fff;
}
.x6-menu {
  position: relative;
  display: inline-block;
  min-width: 120px;
  // min-height: 32px;
  margin: 0;
  padding: 4px 0;
  background-color: #fff;
  outline: 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
}
</style>